//页面加载完成
$(function () {

    //抽取变量
    var issuepolicy_datagrid = $("#issuepolicy_datagrid"); //表格数据
    var issuepolicy_tabs = $("#issuepolicy_tabs"); //工具条
    var issuepolicy_dialog = $("#issuepolicy_dialog");//弹框
    var issuepolicy_form = $("#issuepolicy_form"); //表单信息

    //渲染查看表单中的数据列表
    $("#product_datagrid").datagrid({
        columns:[[
            {field:'name',title:'险种',width:100},
            {field:'annuaflee',title:'价格',width:100}
        ]]
    });
    //初始产品信息界面
    issuepolicy_datagrid.datagrid({
        fit: true,
        fitColumns: true,
        striped: true,
        url:"/issuepolicy/list",
        pagination: true,
        singleSelect: true,
        rownumbers:true,
        toolbar:"#issuepolicy_tabs",
        columns: [[
            {field:'customer',title:'投保人',width:70,align: 'center',
                formatter:function (value, row, index) {
                    return value?value.name:'';
                }
            },
            {field: 'sn', title: '核保单号', width: 150,
                formatter:function (value,row,index) {
                    return row.policy ? row.policy.sn : '';
                }
            },
            {field:'totalAmount',title:'需缴金额',width:70,align: 'center'},
            {field: 'state', title: '缴费状态', width: 100,
                formatter:function (value,row,index) {
                    if(row.policy.state == 0){
                        return '<font style="color:green">暂存状态</font>';
                    }
                    if(row.policy.state == 1){
                        return '<font style="color:yellow">待审核</font>';
                    }
                    if(row.policy.state == 2){
                        return '<font style="color:blue">申请缴费</font>';
                    }
                    if(row.policy.state == 3){
                        return '<font style="color:orange">待修改</font>';
                    }
                    if(row.policy.state == 4){
                        return '<font style="color:green">缴费成功</font>';
                    }
                    if(row.policy.state == 5){
                        return '<font style="color:red">拒保</font>';
                    }
                }
            },
            {field:'duration',title:'保险年限/年',width:70,align: 'center',
                formatter:function (value,row,index) {
                    return row.policy ? row.policy.duration :'';
                }
            },
            {field:'checkDate',title:'核保日期',width:70,align: 'center',
                formatter:function (value,row,index) {
                    return row.policy ? row.policy.checkDate : '';
                }
            },
            {field: 'safetymechanism', title: '保险机构', width: 100,
                formatter:function (value,row,index) {
                    return value?value.name:'';
                }
            }
        ]]
    });

    //将方法绑定到对象中去
    var cmdObj = {
        //刷新功能
        reloadIssue:function () {
            issuepolicy_datagrid.datagrid("reload");
        },

        //取消按钮功能
        cancelIssue:function () {

            issuepolicy_dialog.dialog("close");
        },
        //缴费功能
        pay:function () {
            //获取选中行
            var row = issuepolicy_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert("温馨提示", "老铁请选择要查看的数据", "error");
                return;
            }
            issuepolicy_dialog.dialog("setTitle","信息列表");
            issuepolicy_dialog.dialog("open");
            issuepolicy_form.form("clear");

            //数据回显
            if (row.customer) {
                row["customer.name"] = row.customer.name;
            }
            if(row.policy){
                row["policy.sn"] = row.policy.sn;
            }
            if (row.safetymechanism) {
                row["safetymechanism.name"] = row.safetymechanism.name;
            }


            if(row.policy){
                row["policy.sn"] = row.policy.sn;
                row["policy.id"] = row.policy.id;
            }
            //用ajax发送异步请求给后台发送消息,然后获取表单中数据表格的数据
            $.post("/issuepolicy/selectProductBySn",{sn:row.policy.sn},function (data) {
                $("#product_datagrid").datagrid("loadData",data);
            });

            issuepolicy_form.form("load",row);
            $("#date1").datebox("clear");
            $("#date2").datebox("clear");
            $("#st").combobox("clear");
        },
        //查询功能
        doSearch:function () {
            var row = issuepolicy_datagrid.datagrid("getSelected");
            console.log(row);
            //获取高级查询条件
            var keyword = $("#keyword").val();
            var policySn = $("#policySn").val();

            //再次加载数据
            issuepolicy_datagrid.datagrid("load",{keyword:keyword,policySn:policySn});
        },
        //缴费确认功能
        ok:function () {
            issuepolicy_form.form("submit", {
                url: '/issuepolicy/update',
                success: function (data) {
                    //使用easyui的form提交,需要把data转成json对象
                    data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert('温馨提示', data.message, 'info', function () {
                            //$("#form").form('clear');
                            issuepolicy_dialog.dialog('close');
                            //重新加载数据表格
                            issuepolicy_datagrid.datagrid("load");
                        });
                    } else {
                        $.messager.alert('温馨提示', data.message, 'info');
                    }
                }
            })
        }

    };
   //初始化对话框
    issuepolicy_dialog.dialog({
        width: 400,
        height: 500,
        title:'新增信息',
        closed:true,
        buttons: "#pay_buttons"
    });

   //使用js来绑定事件  使用了属性过滤器来完成该操作
    $("a[data-cmd]").click(function(){
        var cmd = $(this).data("cmd");
        //调用方法
        cmdObj[cmd]();
    });


});
